﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.Model.Parameter.Config;
using Kovan.BLL.Config;
using Kovan.Model.Entity.Config;
using Kovan.BLL.Common;
using Kovan.Library.IO;
using Kovan.Library.Json;
using System.IO;

namespace Kovan.UI.Areas.Manage.Controllers
{
    public class AdController : ManageControllerBase
    {
        //
        // GET: /Manage/Ad/

        public ActionResult Index(PAd param)
        {
            if (!(param.PageSize > 0))
            {
                param.PageSize = 20;
            }
            if (!(param.PageIndex > 0))
            {
                param.PageIndex = 1;
            }
            var items = BAd.GetInstances().GetList(param);
            ViewBag.Param = param;
            return View("index", items);
        }


        public ActionResult GetOperate(int? id)
        {

            tb_ad model = new tb_ad();
            if (id.HasValue)
            {
                model = BAd.GetInstances().GetModel(id.Value);
            }
            return View("OperateDialog", model);
        }

        public ContentResult Delete(string ids)
        {
            var statuscode = string.Empty;
            List<string> idss = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var files = new List<string>();
            var result = BAd.GetInstances().BatchDelete(idss, out files);
            foreach (var f in files)
            {
                var path = Server.MapPath("~/Scripts/Ads/" + f);
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
            }
            if (result > 0)
            {
                statuscode = GetSucceedCode("保存成功", "ad_manage", Url.Content("~/Manage/Ad"), "广告管理");
            }
            return Content(statuscode);
        }

        [ValidateInput(false)]
        public ActionResult SaveAd(tb_ad model)
        {
            int result = 0;
            string statuscode = string.Empty;
            string jscontent = "";
            string jsname = "";
            if (model.ad_id.HasValue)
            {
                var model1 = BAd.GetInstances().GetModel(model.ad_id.Value);
                model1.ad_name = model.ad_name;
                model1.description = model.description;
                model1.content = model.content;
                model1.is_enable = model.is_enable;
                if (string.IsNullOrEmpty(model1.file_name))
                {
                    model1.file_name = Guid.NewGuid().ToString("N") + ".js";
                }

                result = BAd.GetInstances().Update(model1);
                jscontent = "document.write('" + model.content.Replace("'", "\\'").Replace("\"", "\\\"") + "');";
                jsname = model1.file_name;
            }
            else
            {
                model.application_id = WebConfig.CurrentUser.ApplicationID;
                model.create_date = DateTime.Now;

                jsname = Guid.NewGuid().ToString("N") + ".js";
                jscontent = "document.write('" + model.content.Replace("'", "\\'").Replace("\"", "\\\"") + "');";

                model.file_name = jsname;
                result = BAd.GetInstances().Add(model);


            }


            FileUtility.WriteFile(Server.MapPath("~/scripts/Ads/" + jsname), jscontent);

            if (result > 0)
            {
                //成功
                statuscode = GetSucceedCode("保存成功", "ad_manage", Url.Content("~/Manage/Ad"), "广告管理");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

    }
}
